//=============================================================================
#include "AppStateCredits.h"

#include "AppStateManager.h"

//=============================================================================
AppStateCredits AppStateCredits::Instance;

//=============================================================================
AppStateCredits::AppStateCredits() {
	Surf_Logo = NULL;
}

//=============================================================================
void AppStateCredits::OnActivate() {
	// Load Simple Logo
	Surf_Logo = CSurface::OnLoad("./images/logo/credits.png");

	CSoundBank::SoundControl.OnLoad("./Sounds/music01.ogg", 0);
	CSoundBank::SoundControl.Play(0, 0);
	StartTime = SDL_GetTicks();
	alpha = 255;
}

//-----------------------------------------------------------------------------
void AppStateCredits::OnDeactivate() {
    if(Surf_Logo) {
        SDL_FreeSurface(Surf_Logo);
	    Surf_Logo = NULL;
    }
}

//-----------------------------------------------------------------------------
void AppStateCredits::OnLoop() {
	//When 5000 cycles has passed, go to menu screen...
	if(StartTime + 10000 < SDL_GetTicks()) {
		CAppStateManager::SetActiveAppState(APPSTATE_MENU);
	}
	//Sets fade in at start
	else if(StartTime < SDL_GetTicks() && StartTime + 2000 > SDL_GetTicks() && alpha > 0) {
		--alpha;
	}
	//Sets fade out value at end
	else if(StartTime + 8000 < SDL_GetTicks() && alpha < 255) {
		++alpha;
	}
}

//-----------------------------------------------------------------------------
void AppStateCredits::OnRender(SDL_Surface* Surf_Display) {
	if(Surf_Logo) {

		SDL_BlitSurface(Surf_Logo, NULL, Surf_Display, 0);

		//Create alpha surface used for fade.
		SDL_Surface* back = SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCALPHA, 640, 480, 32, 0, 0, 0, 255);
		SDL_FillRect(back, 0, SDL_MapRGBA(back->format, 0, 0, 0, 0));
		SDL_SetAlpha(back, SDL_SRCALPHA, alpha);
		SDL_BlitSurface(back, NULL, Surf_Display, 0);
		
		SDL_Flip(Surf_Display);
		SDL_FreeSurface(Surf_Display);
		SDL_FreeSurface(back);
	}
}

//=============================================================================
AppStateCredits* AppStateCredits::GetInstance() {
	return &Instance;
}

//=============================================================================
